#include <hi_efuse.h>
#include <unistd.h>
#include <stdint.h>

uint32_t i_crc32(const uint8_t *data, size_t len)
{
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < len; i++) {
        crc ^= data[i];
        for (int j = 0; j < 8; j++) {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xEDB88320U;
            else
                crc >>= 1;
        }
    }
    return crc ^ 0xFFFFFFFF;
}

void i_get_device_id(char *str_id)
{
    int ret;

    uint8_t die_id[24] = {0};
    ret = hi_efuse_read(HI_EFUSE_DIE_RW_ID, die_id, sizeof(die_id));
    if (ret != 0) {
        printf("Read DIE ID failed, ret=%u\n", ret);
    }

    // printf("DIE ID: ");
    // for (int i = 0; i < sizeof(die_id); i++) {
    //     printf("%02X", die_id[i]);
    // }
    // printf("\n");
    uint32_t crc = i_crc32(die_id, sizeof(die_id));
    snprintf(str_id, 16, "%08X", crc);   // HEX 字符串
    printf("Read DIE ID crrc32 = %s\n", str_id);
}